package com.yhx.tank;

import java.awt.Color;
import java.awt.Graphics;
import java.util.Random;

public class Tank {
	private int x,y;
	public int getX() {
		return x;
	}

	public void setX(int x) {
		this.x = x;
	}

	public int getY() {
		return y;
	}

	public void setY(int y) {
		this.y = y;
	}


	private Dir dir=Dir.DOWN;
	private static final int SPEED=5;
	public static int WIDTH=ResourceMgr.tankD.getWidth();
	public static int HEIGHT=ResourceMgr.tankD.getHeight();
	private boolean moving =false;
	private TankFrame tf=null;
	private boolean living=true;
	
	
	private Random random;
	
	public boolean isMoving() {
		return moving;
	}

	public void setMoving(boolean moving) {
		this.moving = moving;
	}

	public Dir getDir() {
		return dir;
	}

	public void setDir(Dir dir) {
		this.dir = dir;
	}

	
	public Tank(int x,int y,Dir dir,TankFrame tf) {
		super();
		this.x=x;
		this.y=y;
		this.dir=dir;
		this.tf=tf;
	}
	
	public void paint(Graphics g) {
//		Color c=g.getColor();
//		g.setColor(Color.BLUE);
//		g.fillRect(x, y, 50, 50);
//		g.setColor(c);
		
		if(!living) {
			tf.tanks.remove(this);
		}
		
		switch(dir) {
		case LEFT:
			g.drawImage(ResourceMgr.tankL, x, y, null);
			break;
		case RIGHT:
			g.drawImage(ResourceMgr.tankR, x, y, null);
			break;
		case UP:
			g.drawImage(ResourceMgr.tankU, x, y, null);
			break;
		case DOWN:
			g.drawImage(ResourceMgr.tankD, x, y, null);
			break;
			}
	
		move();
	}

	private void move() {
		if(!moving) return;
		// TODO Auto-generated method stub
		switch(dir) {
		case LEFT:
			x-=SPEED;
			break;
			
		case RIGHT:
			x+=SPEED;
			break;
			
		case UP:
			y-=SPEED;
			break;
			
		case DOWN:
			y+=SPEED;
			break;
		}	
	}
	
	
	//绘画子弹
	public void fire() {
		int bx=this.x+Tank.WIDTH/2-Bullet.WIDTH/2;
		int by=this.y+Tank.HEIGHT/2-Bullet.HEIGHT/2;
		tf.bullets.add(new Bullet(bx,by,this.dir,this.tf));
	}

	public void die() {
		living =false;
		// TODO Auto-generated method stub
		
	}
}
